package com.gxa.dr2.controller;

import com.github.pagehelper.util.StringUtil;
import com.gxa.dr2.dto.LayDTO;
import com.gxa.dr2.dto.Response;
import com.gxa.dr2.dto.ResultDTO;
import com.gxa.dr2.mapper.KeyDao;
import com.gxa.dr2.param.MyParam;
import com.gxa.dr2.pojo.Key;
import com.gxa.dr2.pojo.SalePerformance;
import com.gxa.dr2.service.KeyService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Date;

@Controller
@RequestMapping("/key")
public class KeyController {
    @Resource
    private KeyService keyService;

    @GetMapping("/page")
    public String showKey() {
        return "/key/index.html";
    }

    @ResponseBody
    @PostMapping("/list/data")
    public LayDTO allKey(MyParam param) {
        return keyService.list(param);
    }

    @ResponseBody
    @GetMapping("/detail/{keyId}")
    public ResultDTO allKey(@PathVariable("keyId") Integer keyId) {
        System.out.println(keyId);
        return Response.success(keyService.selectByPrimaryKey(keyId));
    }

    @ResponseBody
    @PostMapping("/add")
    public ResultDTO addKey(@RequestBody Key key, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Integer emplId = (Integer) session.getAttribute("emplId");
        key.setEmplId(emplId);
        keyService.insertSelective(key);
        return Response.success();
    }

    @ResponseBody
    @PostMapping("/delete")
    public ResultDTO deleteKey(Integer performanceId) {
        keyService.deleteByPrimaryKey(performanceId);
        return Response.success();
    }

    @ResponseBody
    @PostMapping("/delete/all")
    public ResultDTO deleteAllKey(@RequestBody Integer[] ids) {
        keyService.deleteByIds(ids);
        return Response.success();
    }

    @ResponseBody
    @PostMapping("/edit")
    public ResultDTO editKey(@RequestBody Key key) {
        if (key != null) {
            key.setUpdateTime(new Date());
            keyService.updateByPrimaryKeySelective(key);
        }
        return Response.success();
    }

    @GetMapping("/add/page")
    public String addKeyPage() {
        return "/key/add.html";
    }

    @GetMapping("/edit/page")
    public String editKeyPage() {
        return "/key/edit.html";
    }
}
